package com.ylbh.modules.member.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.exception.NormalException;
import com.ylbh.common.pojo.business.dto.BusinessPicDTO;
import com.ylbh.common.pojo.member.dto.*;
import com.ylbh.common.pojo.member.dto.select.ExtractMoneyDetailSelectDTO;
import com.ylbh.common.pojo.member.dto.select.MemberSelectDTO;
import com.ylbh.common.pojo.member.dto.select.MerchantIncomeSelectDTO;
import com.ylbh.common.pojo.member.dto.update.MemberUpdateDTO;
import com.ylbh.common.pojo.member.vo.*;
import com.ylbh.common.utils.*;
import com.ylbh.modules.business.mapper.ShoppingUserMapper;
import com.ylbh.modules.member.mapper.MemberMapper;
import com.ylbh.modules.member.service.MemberService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberMapper, MemberDTO> implements MemberService {

    @Autowired
    private ShoppingUserMapper shoppingUserMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Value("${url.clearUser}")
    private String clearUser;

    /**
     * 获取会员列表
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(MemberSelectDTO params) {
        Page<Object> page = new Page<>(params.getPage(), params.getLimit());
        List<MemberVO> list = memberMapper.selectMemberList(params, page);
        return new PageUtils(new Page<MemberVO>().setTotal(page.getTotal()).setRecords(list));
    }

    /**
     * 通过id获取用户信息
     * @param id
     * @return
     */
    @Override
    public MemberVO getUserById(Long id) {
        MemberVO byUserId = memberMapper.getByUserId(id);
        // 查询授权
        String identifier = memberMapper.selectAuthByUserId(id);
        byUserId.setIdentifier(identifier);
//        if (byUserId.getUserType() == 1 || byUserId.getUserType() == 3 || byUserId.getUserType() == 4 || byUserId.getUserType() == 7) {
//            if (byUserId.getStatus() == 1 || byUserId.getStatus() == 2 || byUserId.getStatus() == 5) {
//                // 账号不正常
//                byUserId.setState(1);
//            }else {
//                // 账号正常
//                byUserId.setState(0);
//            }
//        }
//        if (byUserId.getUserType() == 2 || byUserId.getUserType() == 5 || byUserId.getUserType() == 6 || byUserId.getUserType() == 8) {
//            if (byUserId.getStatus() == 0 || byUserId.getStatus() == 2) {
//                // 账号不正常
//                byUserId.setState(1);
//            }else {
//                // 账号正常
//                byUserId.setState(0);
//            }
//        }
        // 不区分用户的类型，只要status为5则是锁定
        if (byUserId.getStatus() != 5) {
            byUserId.setState(0);
        } else {
            byUserId.setState(1);
        }
        MemberUserTypeVO type = null;
        if (null != byUserId.getParentId() && byUserId.getParentId() != 1) {
            MemberUserTypeVO vo = new MemberUserTypeVO();
            vo.setParentId(byUserId.getParentId());
            type = userType(vo);
        }
        if (null != type && null != type.getUserType())
            byUserId.setUserTypes(type.getUserType());
        return byUserId;
    }

    // 递归去寻找该用户的最上级
    public MemberUserTypeVO userType(MemberUserTypeVO Level) {
        if (null != Level) {
            Level = memberMapper.selectTopLevel(Level.getParentId());
            if (null != Level && null != Level.getParentId()) {
                userType(Level);
            }
        }
        return Level;
    }

    /**
     * 修改用户信息
     * @param memberDTO
     * @return
     */
    @Override
    public void modifyByUserId(MemberDTO memberDTO) {
        // 给用户密码加密处理
        if (!StringUtils.isEmpty(memberDTO.getPassword())) {
            memberDTO.setPassword(MD5.md5(memberDTO.getPassword()));
        }
        // 根据不同类型来改变用户状态
//        if (memberDTO.getUserType() == 1 || memberDTO.getUserType() == 3 || memberDTO.getUserType() == 4 || memberDTO.getUserType() == 7) {
//            if (memberDTO.getStatus() == 1) {
//                memberDTO.setStatus(5);
//            }
//            if (memberDTO.getStatus() == 0) {
//                memberDTO.setStatus(0);
//            }
//        }
//        if (memberDTO.getUserType() == 2 || memberDTO.getUserType() == 5 || memberDTO.getUserType() == 6 || memberDTO.getUserType() == 8) {
//            if (memberDTO.getStatus() == 1) {
//                memberDTO.setStatus(0);
//            }
//            if (memberDTO.getStatus() == 0) {
//                memberDTO.setStatus(1);
//            }
//        }
        // 如果用户类型是运营商、消费者、vip会员、城市合伙人
        if (null != memberDTO.getUserType()) {
            if (memberDTO.getUserType() == 1 || memberDTO.getUserType() == 3 || memberDTO.getUserType() == 4 || memberDTO.getUserType() == 7) {
//            // 如果状态不是正常状态
//            if (memberDTO.getStatus() == 1 || memberDTO.getStatus() == 2 || memberDTO.getStatus() == 5) {
//                // 如果状态是正常的，则修改为0
//                if (memberDTO.getState() == 0) {
//                    memberDTO.setStatus(0);
//                }
//            // 如果是正常的，修改成锁定的
//            }else if(memberDTO.getStatus() == 0){
//                // 则状态改为5
//                if (memberDTO.getState() == 1) {
//                    memberDTO.setStatus(5);
//                }
//            }
                if (memberDTO.getState() == 0) {
                    memberDTO.setStatus(0);
                } else if (memberDTO.getState() == 1) {
                    memberDTO.setStatus(5);
                }
            }
            // 如果类型是联盟店
            if (memberDTO.getUserType() == 2 || memberDTO.getUserType() == 5 || memberDTO.getUserType() == 6 || memberDTO.getUserType() == 8) {
//            // 如果不是正常状态
//            if (memberDTO.getStatus() == 0 || memberDTO.getStatus() == 2) {
//                if (memberDTO.getState() == 0) {
//                    memberDTO.setStatus(1);
//                }
//            }else {
//                memberDTO.setStatus(1);
//            }
                if (memberDTO.getState() == 1) {
                    memberDTO.setStatus(5);
                } else if (memberDTO.getState() == 0) {
                    // 查询修改用户的状态
                    int status = memberMapper.selectStatusById(memberDTO.getId());
                    // 如果将这个用户从锁定改成正常的，刚是审核不通过状态
                    // 如果这个用户本来就是正常状态，则将原来的状态替换
                    if (status != 5) {
                        memberDTO.setStatus(status);
                    } else {
                        memberDTO.setStatus(2);
                    }
                }
            }
        }

        // 修改会员的主图
        if (!StringUtils.isEmpty(memberDTO.getPhotoId())) {
            if (!StringUtils.isEmpty(memberDTO.getMainImgName())) {
                memberDTO.setExt(memberDTO.getMainImgName().substring(memberDTO.getMainImgName().lastIndexOf(".") + 1));
            }
            memberMapper.updateUserByPhotoId(memberDTO);
        }else {
            // 如果用户还没主图，先插入一张图片并返回id，并放入到shopping_user表中photoId
            if(!StringUtils.isEmpty(memberDTO.getMainImgName()) && !StringUtils.isEmpty(memberDTO.getMainImgPath())){
                BusinessPicDTO dto = new BusinessPicDTO();
                dto.setExt(memberDTO.getMainImgName().substring(memberDTO.getMainImgName().lastIndexOf(".") + 1));
                dto.setPath(memberDTO.getMainImgPath());
                dto.setDate(new Date());
                dto.setName(memberDTO.getMainImgName());
                shoppingUserMapper.insertPic(dto);
                memberDTO.setPhotoId(dto.getId().toString());
            }
        }
        try {
            // 清除redis缓存
            String cleanUserUrl = clearUser + memberDTO.getId();
            URL url = new URL(cleanUserUrl);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.connect();
        } catch (IOException e) {
            e.printStackTrace();
        }
        memberMapper.updateByUserId(memberDTO);
    }

    /**
     * 修改用户类型
     * @param upgradeDTO
     */
    @Transactional(rollbackFor = { Exception.class })
    @Override
    public String modifyUserType(MemberUpgradeDTO upgradeDTO) {
        //修改用户类型
//        MemberDTO memberDTO = new MemberDTO();
//        memberDTO.setId(upgradeDTO.getUserId().longValue());
//        memberDTO.setUserType(upgradeDTO.getUpgradeUserType());
//        if (upgradeDTO.getUpgradeUserType() == 1) {
//            memberDTO.setStatus(5);
//        }
        List<Integer> typeList = new ArrayList<>();
        int modifyUserParentId = 0;
        List<MemberVO> subordinateUser = null;
        MemberVO vo = null;
        // 先去查询用户原有的类型
        int userType = memberMapper.selectUserTypeByUserId(upgradeDTO.getUserId());
        if (upgradeDTO.getUpgradeUserType() != userType) {
            // 表示改变了原有的用户类型
            if (upgradeDTO.getMoneyType() == 5) {
                // 给用户进行的是升级的操作
                // 1.先需要查询这个用户的上级类型是否比该用户升级后的类型低
                // 2.如果比该用户低，则往上级追踪
                // 3.如果高则可以直接修改类型
                if (upgradeDTO.getUpgradeUserType() == 4) {
                    // VIP
                    for (int i = 0; i < 8; i++) {
                        if (i == 0) {
                            vo = memberMapper.selectSuperiorUserTypeByUserId(upgradeDTO.getUserId());
                        } else {
                            vo = memberMapper.selectSuperiorUserTypeByUserId(vo.getId().intValue());
                        }
                        if (null != vo) {
                            if (vo.getUserType() != 3 && vo.getUserType() != 4) {
                                // 则可以将升级的用户放在该用户下
                                vo.setParentId(vo.getId().intValue());
                                break;
                            }
                        } else {
                            if (null != upgradeDTO.getSuperiorId()) {
                                vo = new MemberVO();
                                vo.setParentId(upgradeDTO.getSuperiorId());
                                break;
                            }
                        }
                    }
                } else if (upgradeDTO.getUpgradeUserType() == 2) {
                    // 普通联盟店
                    // 查询上级的用户类型
                    for (int i = 0; i < 8; i++) {
                        if (i == 0) {
                            vo = memberMapper.selectSuperiorUserTypeByUserId(upgradeDTO.getUserId());
                        } else {
                            vo = memberMapper.selectSuperiorUserTypeByUserId(vo.getId().intValue());
                        }
                        if (null != vo) {
                            if (vo.getUserType() != 3 && vo.getUserType() != 4 && vo.getUserType() != 2) {
                                // 则可以将升级的用户放在该用户下
                                vo.setParentId(vo.getId().intValue());
                                break;
                            }
                        } else {
                            if (null != upgradeDTO.getSuperiorId()) {
                                vo = new MemberVO();
                                vo.setParentId(upgradeDTO.getSuperiorId());
                                break;
                            }
                        }
                    }
                } else if (upgradeDTO.getUpgradeUserType() == 5) {
                    // 中级联盟店
                    for (int i = 0; i < 8; i++) {
                        if (i == 0) {
                            vo = memberMapper.selectSuperiorUserTypeByUserId(upgradeDTO.getUserId());
                        } else {
                            vo = memberMapper.selectSuperiorUserTypeByUserId(vo.getId().intValue());
                        }
                        if (null != vo) {
                            if (vo.getUserType() != 3 && vo.getUserType() != 4 && vo.getUserType() != 2 && vo.getUserType() != 5) {
                                // 则可以将升级的用户放在该用户下
                                vo.setParentId(vo.getId().intValue());
                                break;
                            }
                        } else {
                            if (null != upgradeDTO.getSuperiorId()) {
                                vo = new MemberVO();
                                vo.setParentId(upgradeDTO.getSuperiorId());
                                break;
                            }
                        }
                    }
                } else if (upgradeDTO.getUpgradeUserType() == 6) {
                    // 高级联盟店
                    for (int i = 0; i < 8; i++) {
                        if (i == 0) {
                            vo = memberMapper.selectSuperiorUserTypeByUserId(upgradeDTO.getUserId());
                        } else {
                            vo = memberMapper.selectSuperiorUserTypeByUserId(vo.getId().intValue());
                        }
                        if (null != vo) {
                            if (vo.getUserType() == 7 || vo.getUserType() == 8 || vo.getUserType() == 1) {
                                // 则可以将升级的用户放在该用户下
                                vo.setParentId(vo.getId().intValue());
                                break;
                            }
                        } else {
                            if (null != upgradeDTO.getSuperiorId()) {
                                vo = new MemberVO();
                                vo.setParentId(upgradeDTO.getSuperiorId());
                                break;
                            }
                        }
                    }
                } else if (upgradeDTO.getUpgradeUserType() == 7 || upgradeDTO.getUpgradeUserType() == 8) {
                    // 城市合伙人或者城市合伙人联盟店
                    for (int i = 0; i < 8; i++) {
                        if (i == 0) {
                            vo = memberMapper.selectSuperiorUserTypeByUserId(upgradeDTO.getUserId());
                        } else {
                            vo = memberMapper.selectSuperiorUserTypeByUserId(vo.getId().intValue());
                        }
                        if (null != vo) {
                            if (vo.getUserType() == 1 ) {
                                // 则可以将升级的用户放在该用户下
                                vo.setParentId(vo.getId().intValue());
                                break;
                            }
                        } else {
                            if (null != upgradeDTO.getSuperiorId()) {
                                vo = new MemberVO();
                                vo.setParentId(upgradeDTO.getSuperiorId());
                                break;
                            }
                        }
                    }
                }
            } else if (upgradeDTO.getMoneyType() == 4) {
                // 被降级用户当前的上级
                modifyUserParentId = memberMapper.selectModifyUserParentId(upgradeDTO.getUserId());
                // 给用户进行的是降级的操作
                // 降级后该用户的下级比它高，则将比它高的下级直接放在1的下级
                vo = new MemberVO();
                if (upgradeDTO.getPassiveUserType() == 1) {
                    // 说明降级前是运营商
                    return "运营商不能降级";
                } else if (upgradeDTO.getPassiveUserType() == 2) {
                    // 说明降级前是普通联盟店,降级后有下级高于他，则将高于他的下级放在他操作前的上级下
                    if (upgradeDTO.getUpgradeUserType() == 4) {
                        // 普通联盟店->VIP
                        // 查出所有下级是vip的
                        typeList.add(2);
                        typeList.add(4);
                        subordinateUser = memberMapper.selectSubordinateUserByIdAndType(upgradeDTO.getUserId(), typeList);

                    } else if (upgradeDTO.getUpgradeUserType() == 3) {
                        // 普通联盟店->普通会员
                        subordinateUser = memberMapper.selectSubordinateUserById(upgradeDTO.getUserId());
                    } else {
                        return "降级失败";
                    }

                } else if (upgradeDTO.getPassiveUserType() == 5) {
                    // 说明降级前是中级版联盟店,降级后有下级高于他，则将高于他的下级放在他操作前的上级下
                    if (upgradeDTO.getUpgradeUserType() == 2) {
                        // 中级版联盟店->普通联盟店
                        typeList.add(2);
                        typeList.add(5);
                        subordinateUser = memberMapper.selectSubordinateUserByIdAndType(upgradeDTO.getUserId(), typeList);
                    } else if (upgradeDTO.getUpgradeUserType() == 4) {
                        // 中级版联盟店->VIP
                        typeList.add(2);
                        typeList.add(5);
                        typeList.add(4);
                        subordinateUser = memberMapper.selectSubordinateUserByIdAndType(upgradeDTO.getUserId(), typeList);
                    } else if (upgradeDTO.getUpgradeUserType() == 3) {
                        // 中级版联盟店->普通会员
                        typeList.add(2);
                        typeList.add(5);
                        typeList.add(4);
                        typeList.add(3);
                        subordinateUser = memberMapper.selectSubordinateUserById(upgradeDTO.getUserId());
                    } else {
                        return "降级失败";
                    }

                } else if (upgradeDTO.getPassiveUserType() == 6) {
                    // 说明降级前是高级版联盟店,降级后有下级高于他，则将高于他的下级放在他操作前的上级下
                    if (upgradeDTO.getUpgradeUserType() == 5) {
                        // 高级版联盟店->中级联盟店
                        typeList.add(6);
                        typeList.add(5);
                        subordinateUser = memberMapper.selectSubordinateUserByIdAndType(upgradeDTO.getUserId(), typeList);
                    } else if (upgradeDTO.getUpgradeUserType() == 2) {
                        // 高级版联盟店->普通联盟店
                        typeList.add(6);
                        typeList.add(5);
                        typeList.add(2);
                        subordinateUser = memberMapper.selectSubordinateUserByIdAndType(upgradeDTO.getUserId(), typeList);
                    } else if (upgradeDTO.getUpgradeUserType() == 4) {
                        // 高级版联盟店->VIP
                        typeList.add(6);
                        typeList.add(5);
                        typeList.add(2);
                        typeList.add(4);
                        subordinateUser = memberMapper.selectSubordinateUserByIdAndType(upgradeDTO.getUserId(), typeList);
                    } else if (upgradeDTO.getUpgradeUserType() == 3) {
                        // 高级版联盟店->普通会员
                        typeList.add(6);
                        typeList.add(5);
                        typeList.add(2);
                        typeList.add(4);
                        typeList.add(3);
                        subordinateUser = memberMapper.selectSubordinateUserById(upgradeDTO.getUserId());
                    } else {
                        return "降级失败";
                    }

                } else if (upgradeDTO.getPassiveUserType() == 7 || upgradeDTO.getPassiveUserType() == 8) {
                    // 说明降级前是城市合伙人,降级后有下级高于他，则将高于他的下级放在他操作前的上级下
                    if (upgradeDTO.getUpgradeUserType() == 6) {
                        // 合伙人->高级版联盟店
                        typeList.add(6);
                        typeList.add(7);
                        subordinateUser = memberMapper.selectSubordinateUserByIdAndType(upgradeDTO.getUserId(), typeList);
                    } else if (upgradeDTO.getUpgradeUserType() == 5) {
                        // 合伙人->中级联盟店
                        typeList.add(5);
                        typeList.add(6);
                        typeList.add(7);
                        subordinateUser = memberMapper.selectSubordinateUserByIdAndType(upgradeDTO.getUserId(), typeList);
                    } else if (upgradeDTO.getUpgradeUserType() == 2) {
                        // 合伙人->普通联盟店
                        typeList.add(2);
                        typeList.add(5);
                        typeList.add(6);
                        typeList.add(7);
                        subordinateUser = memberMapper.selectSubordinateUserByIdAndType(upgradeDTO.getUserId(), typeList);
                    } else if (upgradeDTO.getUpgradeUserType() == 4) {
                        // 合伙人->VIP
                        typeList.add(2);
                        typeList.add(4);
                        typeList.add(5);
                        typeList.add(6);
                        typeList.add(7);
                        subordinateUser = memberMapper.selectSubordinateUserByIdAndType(upgradeDTO.getUserId(), typeList);
                    } else if (upgradeDTO.getUpgradeUserType() == 3) {
                        // 合伙人->普通会员
                        subordinateUser = memberMapper.selectSubordinateUserById(upgradeDTO.getUserId());
                    } else {
                        return "降级失败";
                    }

                } else if (upgradeDTO.getPassiveUserType() == 4) {
                    // 说明降级前是vip,将所有的下级全部给操作前的上级
                    if (upgradeDTO.getUpgradeUserType() == 3) {
                        // VIP->普通会员
                        subordinateUser = memberMapper.selectSubordinateUserById(upgradeDTO.getUserId());
                    } else {
                        return "降级失败";
                    }
                } else {
                    // 就是消费者
                    return "降级类型不能同级";
                }
            }
            if (null != vo.getId()) {
                vo.setParentId(vo.getId().intValue());
            }
            vo.setId(upgradeDTO.getUserId().longValue());
            vo.setUserType(upgradeDTO.getUpgradeUserType());
            if (null != upgradeDTO.getUserId()) {
                // 修改用户类型并更换上级用户
                try {

                    //插入升级数据

                    // 如果升降级到联盟店则改为资料未完善状态
                    if (upgradeDTO.getUpgradeUserType() == 2 || upgradeDTO.getUpgradeUserType() == 5 || upgradeDTO.getUpgradeUserType() == 6) {
                        vo.setStatus(3);
                    }
                    if (upgradeDTO.getMoneyType() == 4) {
                        memberMapper.updateUserTypeByUserId(vo);
                    } else {
                        if(null != upgradeDTO.getSuperiorId()) {
                            vo.setParentId(upgradeDTO.getSuperiorId());
                        }
                        memberMapper.updateUserTypeOrParentIdByUserId(vo);
                    }
                    memberMapper.saveUpgrade(upgradeDTO);

                    if (upgradeDTO.getMoneyType() == 4 && subordinateUser.size() > 0) {
                        // 修改同级和高于降级后类型的用户
                        memberMapper.updateSuperior(subordinateUser, modifyUserParentId);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    // 事务回滚
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return "修改类型错误";
                }

                return "修改类型成功";
            } else {
                return "修改类型失败";
            }
        } else {
            return "用户已是该类型";
        }

//        memberMapper.updateByUserId(memberDTO);

    }

    @Override
    public void modifyById(MemberUpdateDTO memberUpdateDTO) {
        memberMapper.updateById(memberUpdateDTO);
    }

    /**
     * 提现明细列表
     * @param params
     * @return
     */
    @Override
    public PageUtils queryBusinessWithRecordList(ExtractMoneyDetailSelectDTO params) {
        return new PageUtils(memberMapper.selectBusinessWithRecordList(params),
                memberMapper.queryBusinessWithRecordCount(params),
                params.getLimit(),
                params.getPage());
    }

    /**
     * 获取商户收益列表
     * @param params
     * @return
     */
    @Override
    public PageUtils queryMerchantIncomeList(MerchantIncomeSelectDTO params) {
        Page<Object> page = new Page<>(params.getPage(), params.getLimit());
        List<MerchantIncomeDTO> list = memberMapper.selectMerchantIncomeList(params, page);
        return new PageUtils(new Page<MerchantIncomeDTO>().setTotal(page.getTotal()).setRecords(list));
    }

    /**
     * 汇总商户总金额
     * @param id
     * @return
     */
    @Override
    public BigDecimal sumMerchantIncomeMoney(Long id) {
        BigDecimal bigDecimal = memberMapper.sumMerchantIncomeMoney(id);
        if(bigDecimal == null){
            bigDecimal = BigDecimal.valueOf(0.00);
        }
        return bigDecimal;
    }

    /**
     * 汇总商户推广提现未提现余额
     * @param id
     * @return
     */
    @Override
    public HashMap<String, BigDecimal> sumMerchantExtensionMoney(Long id) {
        return memberMapper.sumMerchantExtensionMoney(id);
    }

    /**
     * 通过用户名获取用户
     * @param name
     * @return
     */
    @Override
    public MemberVO getByUserName(String name) {
        return memberMapper.getByUserName(name);
    }

    /**
     * 运营商列表
     * @param params
     * @return
     */
    @Override
    public PageUtils queryOperatorPage(PageDTO params) {
//        return new PageUtils(memberMapper.selectOperatorList(params),
//                memberMapper.getOperatorUserCount(params),
//                params.getLimit(),
//                params.getPage());
        Page<Object> page = new Page<>(params.getPage(), params.getLimit());
        List<OperatorVO> list = memberMapper.selectOperatorList(params, page);
        return new PageUtils(new Page<OperatorVO>().setTotal(page.getTotal()).setRecords(list));
    }

    /**
     * 导出会员详情
     * @param response
     * @param request
     */
    @Override
    public void userExcel(HttpServletResponse response, HttpServletRequest request) throws NormalException {
        String userType = request.getParameter("userType");
        String keywords = request.getParameter("keywords");
        String province = request.getParameter("province");
        String city = request.getParameter("city");
        String town = request.getParameter("town");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        List<UserExcelVO> excel = memberMapper.selectUserExcel(userType, keywords, province, city, town, startTime, endTime);
        ExcelUtils.exportExcel(excel, "导出会员详情", "", UserExcelVO.class, System.currentTimeMillis() + ".xls", response);
    }


    /**
     * 新增运营商
     * @param addOperatorDTO
     */
    @Override
    public void addOperator(AddOperatorDTO addOperatorDTO) {
        addOperatorDTO.setDate(new Date());
        addOperatorDTO.setFroms("后台添加");
        addOperatorDTO.setRegisterType("artificial");
        addOperatorDTO.setPassword(MD5.md5("000000"));
        addOperatorDTO.setStatus(5);
        addOperatorDTO.setUserType(1);
        memberMapper.insertOperator(addOperatorDTO);
        // 增加操作记录
        addOperatorDTO.setOperatorName(ShiroUtils.getUserEntity().getUsername());
        addOperatorDTO.setContent("后台添加新运营商，运营商ID为" + addOperatorDTO.getId());
        addOperatorDTO.setFhLogId(UuidUtil.get32UUID());
        memberMapper.insertSysFhLog(addOperatorDTO);
    }

    /**
     * 修改购物劵
     * @param userMemberDTO
     */
    @Transactional
    @Override
    public void updateUserIntegralById(MemberDTO userMemberDTO) {
        memberMapper.updateUserIntegralById(userMemberDTO.getId(),userMemberDTO.getIntegral());
    }

    /**
     * 会员关系
     * @param dto
     * @return
     */
    @Override
    public PageUtils queryMembership(MembershipDTO dto) {
        Page<Object> page = new Page<>(dto.getPage(), dto.getLimit());
        List<MembershipVO> membership = memberMapper.selectMembership(dto, page);
        return new PageUtils(new Page<MembershipVO>().setRecords(membership).setTotal(page.getTotal()));
    }

    /**
     * 获取运营商是否添加过
     * @param userName
     * @return
     */
    @Override
    public int getIsExistenceByUserName(String userName) {
        return memberMapper.selectIsExistenceByUserName(userName);
    }

    /**
     * 会员升级-判断是否需要选择上级
     * @param id
     * @return
     */
    @Override
    public MemberVO needToSelectSuperiorById(Long id) {
        Boolean isSelect = true;
        MemberVO vo = new MemberVO();
        vo.setId(id);
        while (isSelect) {
            if (null != vo && vo.getId() != 1) {
                MemberVO vo1 = memberMapper.selectSuperiorUserTypeByUserId(vo.getId().intValue());
                if (null != vo1) {
                    if (vo1.getId() != 1) {
                        vo = vo1;
                    } else {
                        isSelect = false;
                    }
                } else {
                    isSelect = false;
                }
            } else {
                isSelect = false;
            }
        }
        return vo;
    }

    /**
     * 获取所有的运营商
     * @return
     */
    @Override
    public List<MemberVO> queryAllOperator() {
        return memberMapper.selectAllOperator();
    }

    /**
     * 查询出所有的合伙人
     * @return
     */
    @Override
    public List<MemberVO> queryAllPartnerList() {
        return memberMapper.selectAllPartnerList();
    }
}
